#include "chat.h"
#include "ui_chat.h"

Chat::Chat(QTcpSocket *s, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Chat)
{
    ui->setupUi(this);
    socket=s;

    //服务器发出信号，客户端接收显示
    connect(socket,&QTcpSocket::readyRead,this,&Chat::serverInfoSolt);
}

Chat::~Chat()
{
    delete ui;
}

void Chat::on_ClearButton_clicked()
{
    ui->lineEdit->clear();
}

void Chat::on_sendButton_clicked()
{
    QByteArray ba;
    ba.append(ui->lineEdit->text());//将QString转换为QByteArray类型
    socket->write(ba);
}
//获取服务器的数据
void Chat::serverInfoSolt()
{
    //获取信号的发出者
    QTcpSocket *s = (QTcpSocket *)sender();
    str += s->readAll();
    str += "\r\n";
    ui->recvtextEdit->setText(str);
    //ui->recvLineEdit->setText(s->readAll());
}
